package com.gitee.wsl.struct.router

/*import java.util.regex.Matcher
import java.util.regex.Pattern*/

/*class UriResource<Context>(
    val baseUri: String,
    val method: Method = Method.GET,
    priority: Int = 0,
    val handler: CallHandler<Context>? = null,
    vararg initParameter1: Any
) : Comparable<UriResource<Context>> {
    val uri: String
    private var uriPattern: Pattern
    private var priority = 0
    val initParameter: Array<Any>
    private val uriParams: MutableList<String> = ArrayList()

    init {
        this.priority = priority + uriParams.size * 1000
        this.initParameter = arrayOf(initParameter1)
        this.uri = normalizeUri(baseUri)
        parse()
        uriPattern = createUriPattern()
    }

    constructor(uri: String,handler: CallHandler<Context>? = null):this(uri,
        Method.ALL,priority=0,handler=handler)

    fun parse() {}

    private fun createUriPattern(): Pattern {
        var patternUri = baseUri
        var matcher: Matcher = PARAM_PATTERN.matcher(patternUri)
        var start = 0
        while (matcher.find(start)) {
            uriParams.add(patternUri.substring(matcher.start() + 1, matcher.end()))
            patternUri = StringBuilder(patternUri.substring(0, matcher.start())) //
                .append(PARAM_MATCHER) //
                .append(patternUri.substring(matcher.end())).toString()
            start = matcher.start() + PARAM_MATCHER.length
            matcher = PARAM_PATTERN.matcher(patternUri)
        }
        return Pattern.compile(patternUri)
    }

    fun process(context: Context,urlParams: Map<String, String> = emptyMap()) {
       handler?.call(context,this,urlParams)
    }


    override fun toString(): String {
        return StringBuilder("UrlResource{uri='").append(baseUri ?: "/") //
            .append("', urlParts=").append(uriParams) //
            .append('}') //
            .toString()
    }


    fun match(url: String): Map<String, String>? {
        val matcher = uriPattern.matcher(url)
        return if (matcher.matches()) {
            if (uriParams.size > 0) {
                val result: MutableMap<String, String> = HashMap()
                for (i in 1..matcher.groupCount()) {
                    result[uriParams[i - 1]] = matcher.group(i)?:""
                }
                result
            } else {
                emptyMap()
            }
        } else null
    }

    override operator fun compareTo(other: UriResource<Context>): Int {
        return if (method.ordinal < other.method.ordinal) {
            1
        } else if (priority > other.priority) {
            1
        } else if (priority < other.priority) {
            -1
        } else {
            0
        }
    }

    fun setPriority(priority: Int) {
        this.priority = priority
    }

}*/

class UriResource<Context>(
    val baseUri: String,
    val method: Method = Method.GET,
    priority: Int = 0,
    val handler: CallHandler<Context>? = null,
    vararg initParameter1: Any
) : Comparable<UriResource<Context>> {
    val uri: String
    private var uriPattern: Regex
    private var priority = 0
    val initParameter: Array<Any>
    private val uriParams: MutableList<String> = ArrayList()

    init {
        this.priority = priority + uriParams.size * 1000
        this.initParameter = arrayOf(initParameter1)
        this.uri = normalizeUri(baseUri)
        parse()
        uriPattern = createUriPattern()
    }

    constructor(uri: String,handler: CallHandler<Context>? = null):this(uri,
        Method.ALL,priority=0,handler=handler)

    fun parse() {}

    private fun createUriPattern(): Regex {
        /*var patternUri = baseUri
        var matcher = PARAM_PATTERN
        var start = 0
        while (matcher.find(patternUri,start)) {
            uriParams.add(patternUri.substring(matcher.start() + 1, matcher.end()))
            patternUri = StringBuilder(patternUri.substring(0, matcher.start())) //
                .append(PARAM_MATCHER) //
                .append(patternUri.substring(matcher.end())).toString()
            start = matcher.start() + PARAM_MATCHER.length
            matcher = PARAM_PATTERN.matcher(patternUri)
        }
        return Pattern.compile(patternUri)*/

        return baseUri.replace(PARAM_PATTERN){
            uriParams.add(it.value)
            PARAM_MATCHER
        }.toRegex()
    }

    fun process(context: Context,urlParams: Map<String, String> = emptyMap()) {
        handler?.call(context,this,urlParams)
    }


    override fun toString(): String {
        return StringBuilder("UrlResource{uri='").append(baseUri ?: "/") //
            .append("', urlParts=").append(uriParams) //
            .append('}') //
            .toString()
    }


    fun match(url: String): Map<String, String>? {
        /*val matcher = uriPattern.matcher(url)
        return if (matcher.matches()) {
            if (uriParams.size > 0) {
                val result: MutableMap<String, String> = HashMap()
                for (i in 1..matcher.groupCount()) {
                    result[uriParams[i - 1]] = matcher.group(i)?:""
                }
                result
            } else {
                emptyMap()
            }
        } else null*/

        return uriPattern.matchEntire(url)?.let{
            val result: MutableMap<String, String> = HashMap()
            it.groupValues.forEachIndexed { i, value ->
                println("index:$i ->$value")
                if(i in 1.. uriParams.size)
                    result[uriParams[i-1]] = value
            }
            result
        }
    }

    override operator fun compareTo(other: UriResource<Context>): Int {
        return if (method.ordinal < other.method.ordinal) {
            1
        } else if (priority > other.priority) {
            1
        } else if (priority < other.priority) {
            -1
        } else {
            0
        }
    }

    fun setPriority(priority: Int) {
        this.priority = priority
    }

}